<template>
  <div :id="id" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from "echarts";
export default {
  props: {
    id: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "400px",
    },
    data: {
      type: Object,
      default: () => {},
    },
    title: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      chart: null,
      legendData: [],
      echartData: [],
      resizeTimer: null,
    };
  },
  mounted() {
    this.initChart();
    window.addEventListener("resize", this.erchartResize);
    console.log(this.data)
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart = null;
  },
  methods: {
    erchartResize() {
      if (this.resizeTimer) clearTimeout(this.resizeTimer);
      this.resizeTimer = setTimeout(() => {
        this.chart.resize();
      }, 100);
    },
    initChart() {
      this.chart = echarts.init(document.getElementById(this.id));
      const option = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            // 坐标轴指示器，坐标轴触发有效
            type: "shadow", // 默认为直线，可选为：'line' | 'shadow'
          },
        },
        xAxis: [
          {
            type: "category",
            data: this.data.dateData,
            axisTick: {
              alignWithLabel: true,
            },
          },
        ],
        yAxis: [
          {
            type: "value",
            name: "平均分",
            min: 0,
            max: 100,
            axisLabel: {
              formatter: "{value}分",
            },
          },
          {
            type: "value",
            name: "参考人数",
            axisLabel: {
              formatter: "{value}人",
            },
          },
        ],
        dataZoom: [
          {
            show: true,
            realtime: true,
            start: 1,
            end: 8,
            height: 20,
            bottom: 5,
            textStyle: { color: "#333" },
          },
        ],
        series: [
          {
            name: "平均分",
            type: "line",
            itemStyle: {
                normal: {
                    color: '#ea6f21',
                    lineStyle: {
                        color: '#ea6f21',
                    }
                }
            },
            
            data:this.data.scoreData,
          },
          {
            name: "参考人数",
            type: "bar",
            barWidth: "60%",
            yAxisIndex: 1,
            itemStyle: {
                normal: {
                    color: "#3398DB",
                    lineStyle: {
                        color: "#3398DB"
                    }
                }
            },
            data:this.data.examNumber,
          },
        ],
      };
      this.chart.setOption(option);
    },
  },
  destroyed() {
    window.removeEventListener("resize", this.erchartResize);
  },
};
</script>
<style lang='stylus' scoped>
.chartsClass {
  padding-left: 1.2rem;
}
</style>
</script>